package com.webchat.act.controller;

import com.webchat.act.service.CommentService;
import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.CommentSaveVO;
import com.webchat.domain.vo.response.CommentResponseVO;
import com.webchat.rmi.act.ICommentClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class CommentController implements ICommentClient {


    @Autowired
    private CommentService commentService;

    /**
     * 保存评论
     * @param vo
     * @return
     */
    @PostMapping("/addComment")
    public APIResponseBean<Long> saveComment(@RequestBody CommentSaveVO vo) {
        String authorId = SessionHelper.getCurrentUserId();
        vo.setAuthorId(authorId);
        Long commentId = commentService.saveComment(vo);
        return APIResponseBeanUtil.success(commentId);
    }

    /**
     * 删除评论
     * @return
     */
    public APIResponseBean deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id, SessionHelper.getCurrentUserId());
        return APIResponseBeanUtil.success();
    }

    public APIResponseBean<Long> topComment(@PathVariable Long commentId) {
        commentService.topComment(commentId);
        return APIResponseBeanUtil.success("OK");
    }

    public APIResponseBean<Long> cancelTopComment(@PathVariable Long commentId) {
        commentService.cancelTopComment(commentId);
        return APIResponseBeanUtil.success("OK");
    }

    /***
     * 查询资源评论列表
     * @param resourceId
     * @param resourceType
     * @param isHot
     * @param isNew
     * @param pageNo
     * @param pageSize
     * @return
     */
    public APIPageResponseBean<List<CommentResponseVO>> queryComments(@RequestParam(value = "resourceId", required = false) Long resourceId,
                                                                      @RequestParam(value = "resourceType", required = false) String resourceType,
                                                                      @RequestParam(value = "content", required = false) String content,
                                                                      @RequestParam(value = "isHot", required = false, defaultValue = "false") Boolean isHot,
                                                                      @RequestParam(value = "isNew", required = false, defaultValue = "false") Boolean isNew,
                                                                      @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        String currentUserId = SessionHelper.getCurrentUserId();
        return commentService.queryCommentByCondition(currentUserId, content, resourceId, resourceType, pageNo, pageSize, isHot, isNew);
    }

}
